package com.example.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.URLEncoder;

public class SignUtil {
    public static void main(String[] args) throws IOException {
        String getUrl = "http://tieba.baidu.com/dc/common/tbs";
        String cookie = "TIEBA_USERTYPE=4744b33ba8f1b62d3e20539b; bdshare_firstime=1480848177351; BAIDUID=C0E78F34EC260834FB0230B6EFADD316:FG=1; PSTM=1492697855; BIDUPSID=B9DE8996682C32B60092B42F64CEA9A2; FP_UID=5485266b09c685e6971f677eab53cce0; TIEBAUID=1d0e243f3465b9b333cf4113; FP_LASTTIME=1509455648510; MCITY=-179%3A; H_PS_PSSID=1444_21097_17001_22158; rpln_guide=1; PSINO=5; FP_UID=b66fefd4d7d9dda2b6e078f2c3746421; BDUSS=GRXVGlmOE5tMlRoYVJKQWFmeXNyNzhJbWFtQ1RCQXdVcVpVclhTeTNRbERFZEphQVFBQUFBJCQAAAAAAAAAAAEAAAAZo9U61eZUTUTX39TLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEOEqlpDhKpab; cflag=15%3A3; STOKEN=6dbe4f799badd7d69d8470ff62209830d703e4669851918c60531596a72a7558; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1520956385,1521125227; Hm_lpvt_98b9d8c2fd6608d564bf2ac2ae642948=1521127029";
        String postUrl = "http://tieba.baidu.com/sign/add";

        String tbs = get(getUrl, cookie);
        String tiebaName = URLEncoder.encode("梦幻西游","utf-8");
        System.out.println(tiebaName);
        useJsoupSign(postUrl, cookie, tiebaName, tbs);
        System.out.println("over");
    }

    public static void useJsoupSign(String url, String cookie, String tiebaName, String tbs) throws IOException {
        Document doc = Jsoup.connect(url)
                .header("Accept", "application/json, text/javascript, */*; q=0.01")
                .header("Accept-Language", "zh-CN,zh;q=0.8")
                .cookie("Cookie", cookie)
                .timeout(100000)
                .data("ie","utf-8")
                .data("kw",tiebaName)
                .data("tbs",tbs)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36")
                .post();

        JSONObject json = JSONObject.parseObject(doc.text());
        System.out.println(json);
    }

    public static void sign(String url, String cookie, String tiebaName, String tbs) throws IOException {

        NameValuePair charsetNVP = new NameValuePair("ie", "utf-8");
        NameValuePair tiebaNameNVP = new NameValuePair("kw", tiebaName);
        NameValuePair tbsNVP = new NameValuePair("tbs", tbs);
        NameValuePair[] data = {charsetNVP, tiebaNameNVP, tbsNVP};

        post(url, cookie, data, tiebaName);
    }

    public static String get(String url, String cookie) throws IOException {
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);
        // 每次访问需授权的网址时需带上前面的 cookie 作为通行证
        getMethod.setRequestHeader("cookie", cookie);
        getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36");
        httpClient.executeMethod(getMethod);
        String responseStr = getMethod.getResponseBodyAsString();
        System.out.println(responseStr);
        return responseStr;
    }

    public static void post(String url, String cookie, NameValuePair[] data, String tiebaName) throws IOException {
        // 需登陆后访问的 Url
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestHeader("Referer", "http://tieba.baidu.com/f?ie=utf-8&kw=" + tiebaName + "&fr=home");
        postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36");
        postMethod.setRequestHeader("cookie", cookie);
        postMethod.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01");
        postMethod.setRequestHeader("X-Requested-With", "XMLHttpRequest");
//        postMethod.setRequestHeader("Accept-Encoding", "gzip, deflate, br");
        postMethod.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.9");
        postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        postMethod.setRequestBody(data);
        httpClient.executeMethod(postMethod);
        String responseStr = postMethod.getResponseBodyAsString();
        System.out.println(responseStr);
    }
}
